package com.aopcinemas.visitor.behaviour;

import jade.content.ContentElement;
import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.MessageTemplate.MatchExpression;

import com.aopcinemas.visitor.ontology.GetCampaign;
import com.aopcinemas.visitor.ontology.CampaignRequest;
import com.aopcinemas.visitor.ontology.VisitorOntology;

public class VisitorRegistrationBehaviour extends Behaviour {

    private static final long serialVersionUID = 1L;

    boolean done = false;

    @Override
    public void action() {
        // register necessary behavior when visitor sign in network
        myAgent.addBehaviour(new CinemaListUpdateBehaviour(myAgent, 10000));
        myAgent.addBehaviour(new ScheduleSearchBehavior(myAgent));
        myAgent.addBehaviour(new CampaignSearchBehavior(myAgent));

        myAgent.addBehaviour(new CampaignRequestInfoBehavior(myAgent, getRequestCampaignTemplate()));

        myAgent.addBehaviour(new CampaignInfoBehavior(myAgent, getGetCampaignRequestTemplate()));

        done = true;
    }

    private MessageTemplate getGetCampaignRequestTemplate() {
        MessageTemplate perfomative = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);
        MessageTemplate receiver = MessageTemplate.MatchReceiver(new AID[] { myAgent.getAID() });
        MessageTemplate content = new MessageTemplate(new MatchExpression() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean match(ACLMessage msg) {
                try {
                    System.out.println("Check campaign!");
                    if (msg.getOntology() != null && !msg.getOntology().toLowerCase().equals(VisitorOntology.NAME.toLowerCase())) {
                        System.out.println("Ontology not ok!");
                        return false;
                    }
                    System.out.println("Ontology ok!");
                    ContentElement element = myAgent.getContentManager().extractContent(msg);
                    if (!(element instanceof Action)) {
                        System.out.println("Element is not action!");
                        return false;
                    }
                    Action action = (Action) element;
                    System.out.println("Action : " + (action.getAction() instanceof GetCampaign));
                    return action.getAction() instanceof GetCampaign;
                } catch (CodecException e) {
                    e.printStackTrace();
                } catch (OntologyException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });
        MessageTemplate tmp = MessageTemplate.and(perfomative, receiver);
        tmp = MessageTemplate.and(receiver, tmp);
        tmp = MessageTemplate.and(content, tmp);
        return tmp;
    }

    private MessageTemplate getRequestCampaignTemplate() {
        MessageTemplate perfomative = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);
        MessageTemplate receiver = MessageTemplate.MatchReceiver(new AID[] { myAgent.getAID() });
        MessageTemplate content = new MessageTemplate(new MatchExpression() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean match(ACLMessage msg) {
                try {
                    if (msg.getOntology() != null && !msg.getOntology().toLowerCase().equals(VisitorOntology.NAME.toLowerCase())) {
                        return false;
                    }
                    System.out.println("RequestCampaign: " + msg.getOntology() + ", " + msg.getContent());
                    ContentElement element = myAgent.getContentManager().extractContent(msg);
                    if (!(element instanceof Action)) {
                        return false;
                    }
                    Action action = (Action) element;
                    return action.getAction() instanceof CampaignRequest;
                } catch (CodecException e) {
                    e.printStackTrace();
                } catch (OntologyException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });
        MessageTemplate tmp = MessageTemplate.and(perfomative, receiver);
        tmp = MessageTemplate.and(content, tmp);
        return tmp;
    }

    @Override
    public boolean done() {
        return done;
    }

}
